package com.tencent.qf.team.controller;

import com.alibaba.fastjson.JSON;
import com.tencent.qf.common.pojo.HttpResult;
import com.tencent.qf.mybatis.pojo.PageRequest;
import com.tencent.qf.mybatis.pojo.PageResult;
import com.tencent.qf.team.dal.DmAppSalesPersonalPerformance;
import com.tencent.qf.team.dto.request.PerformanceSummaryPersonalListRequest;
import com.tencent.qf.team.dto.request.PersonalPerformanceRequest;
import com.tencent.qf.team.dto.response.PerformanceSummaryPersonalListResponse;
import com.tencent.qf.team.service.PerformanceSummaryService;
import com.tencent.qf.team.service.SalesPerformanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@Api(value = "个人业绩", tags = "个人业绩")
public class SalesPerformancePersionalController {

    @Autowired
    private SalesPerformanceService salesPerformanceService;
    @Autowired
    private PerformanceSummaryService performanceSummaryService;

    @PostMapping("/api/team/performanceSummary/personalList")
    @ApiOperation("个人业绩-列表")
    public HttpResult<PageResult<PerformanceSummaryPersonalListResponse>> personalList(
            @RequestBody PageRequest<PerformanceSummaryPersonalListRequest> request) {
        log.info("个人业绩-列表，入参：{}", JSON.toJSONString(request));

        PerformanceSummaryPersonalListRequest requestData = request.getData();
        List<String> orderFields = Arrays.asList(new String[]{"sales_order_num", "sales_amt", "sales_atv", "sales_user_num",
                "sales_income_amt", "sales_self_income_amt", "sales_promote_income_amt",
                "sales_income_amt_all", "overcharge_sales_income_amt", "promote_income_amt_all", "self_income_amt_all"});
        List<String> orderSorts = Arrays.asList(new String[]{"desc", "asc"});

        HttpResult<PageResult<PerformanceSummaryPersonalListResponse>> result;
        if (!orderFields.contains(requestData.getOrderField())) {
            result = HttpResult.error(500, "排序字段格式错误");
        } else if (!orderSorts.contains(requestData.getOrderDirection())) {
            result = HttpResult.error(500, "排序方式格式错误");
        } else {
            PageResult<PerformanceSummaryPersonalListResponse> data = performanceSummaryService.personalList(request);
            result = HttpResult.success(data);
        }
        log.info("个人业绩-列表，出参：{}", JSON.toJSONString(result));
        return result;
    }

    @PostMapping("/api/team/personal/performance")
    @ApiOperation("个人业绩-批量")
    public HttpResult<List<DmAppSalesPersonalPerformance>> personalPerformance(@RequestBody List<PersonalPerformanceRequest> request) {
        log.info("个人业绩-批量，入参：{}", JSON.toJSONString(request));
        HttpResult<List<DmAppSalesPersonalPerformance>> result = null;
        if (CollectionUtils.isEmpty(request)) {
            result = HttpResult.error(500, "请求参数不能为空");
        } else if (request.size() > 100) {
            result = HttpResult.error(500, "一次查询请求集合元素请勿超过100个");
        } else {
            List<DmAppSalesPersonalPerformance> list = salesPerformanceService.personalPerformance(request);
            result = HttpResult.success(list);
        }
        log.info("个人业绩-批量，出参：{}", JSON.toJSONString(result));
        return result;
    }
}
